package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.chen.common.constants.Constants;
import org.chen.common.constants.PayConstants;
import org.chen.common.constants.SysConfigConstants;
import org.chen.common.exception.ExceptionCodeEnum;
import org.chen.common.exception.FlyingException;
import org.chen.common.model.travel.StoreTravelOrder;
import org.chen.service.StoreTravelOrderService;
import org.chen.service.SystemConfigService;
import org.chen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderUtils {

    @Autowired
    private SystemConfigService systemConfigService;

    /**
     * 检测支付渠道
     * @param payChannel 支付渠道
     */
    public static boolean checkPayChannel(String payChannel) {
       if (!payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_H5) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_PAY_PAL_H5) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_PAY_PAL_PC) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_PROGRAM) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_PUBLIC) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_APP_IOS) &&
               !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_APP_ANDROID)) {
           return false;
       }
       return true;
    }

    /**
     * 检查支付类型
     * @param payType 支付类型标识
     * @return 是否支持
     */
    public Boolean checkPayType(String payType){
        boolean result = false;
        payType = payType.toLowerCase();
        if (PayConstants.PAY_TYPE_PAY_Pal.equals(payType)) {
            result = ("1".equals(systemConfigService.getValueByKey(SysConfigConstants.CONFIG_PAY_PAL_STATUS)));
        }
        return result;
    }

    /**
     * 翻译支付方式给前端
     * @param payType
     * @return
     */
    public String getOrderPayTypeStr(String payType){
        String payTypeStr = null;
        if (Constants.Pay_TYPE_PAY_PAL.equals(payType)) {
            payTypeStr = "PayPal支付";
        } else {
            payTypeStr = "其他支付方式";
        }
        return payTypeStr;
    }

    /**
     * h5 订单查询 where status 封装 （旅游）
     * @param queryWrapper 查询条件
     * @param status 状态
     */
    public void statusTravelApiByWhere(LambdaQueryWrapper<StoreTravelOrder> queryWrapper, Integer status){
        switch (status) {
            case Constants.ORDER_STATUS_H5_UNPAID -> { // 未支付
                queryWrapper.eq(StoreTravelOrder::getPaid, false);
                queryWrapper.eq(StoreTravelOrder::getStatus, 0);
                queryWrapper.eq(StoreTravelOrder::getRefundStatus, 0);
                queryWrapper.eq(StoreTravelOrder::getType, 0);
            }
            case Constants.ORDER_STATUS_H5_NOT_SHIPPED -> { // 待使用
                queryWrapper.eq(StoreTravelOrder::getPaid, true);
                queryWrapper.eq(StoreTravelOrder::getStatus, 1);
                queryWrapper.in(StoreTravelOrder::getRefundStatus, 0, 4);
            }
            case Constants.ORDER_STATUS_H5_COMPLETE -> { // 已完成
                queryWrapper.eq(StoreTravelOrder::getPaid, true);
                queryWrapper.eq(StoreTravelOrder::getStatus, 2);
                queryWrapper.in(StoreTravelOrder::getRefundStatus, 0, 4);
            }
            case Constants.ORDER_STATUS_H5_UNDER_REVIEW -> { // 审核中
                queryWrapper.eq(StoreTravelOrder::getPaid, true);
                queryWrapper.in(StoreTravelOrder::getRefundStatus, 1);
            }
            case Constants.ORDER_STATUS_H5_REFUNDING -> { // 退款中
                queryWrapper.eq(StoreTravelOrder::getPaid, true);
                queryWrapper.in(StoreTravelOrder::getRefundStatus, 3);
            }
            case Constants.ORDER_STATUS_H5_REFUNDED -> { // 已退款
                queryWrapper.eq(StoreTravelOrder::getPaid, true);
                queryWrapper.eq(StoreTravelOrder::getRefundStatus, 2);
            }
            case Constants.ORDER_STATUS_H5_REFUND -> { // 包含已退款和退款中
                queryWrapper.in(StoreTravelOrder::getRefundStatus, 1, 2, 3, 4).or();
                queryWrapper.in(StoreTravelOrder::getStatus, 0, 1, 2, 3);
            }
        }

        if (status.equals(Constants.ORDER_STATUS_H5_CANCEL)){
            //已失效
            queryWrapper.eq(StoreTravelOrder::getIsDel, true);
            queryWrapper.eq(StoreTravelOrder::getIsSystemDel, true);
        }else {
            if (!status.equals(Constants.ORDER_STATUS_H5_REFUND)){
                queryWrapper.eq(StoreTravelOrder::getIsDel, false);
                queryWrapper.eq(StoreTravelOrder::getIsSystemDel, false);
            }
        }
    }

}
